using Domain.Entities;
using Domain.Entities.Enums;
using Domain.Services;
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using UserInterface.Misc;

namespace UserInterface
{
    public partial class EditProfile : BoardPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = CurrentSession.User;
            
            string pageTitle = (string)GetLocalResourceObject("Title");
            MasterPage.TitleGenerator.PageTitle = pageTitle;
            MasterPage.HeaderText = pageTitle;

            MasterPage.AddPathItem("Edit Profile", "EditProfile.aspx");

            if (CurrentSession.IsGuest)
            {
                DieMessage(DieMessageType.Information, "CantEditGuestProfile");
            }
           
            if (!IsPostBack)
            {
                AvatarTextBox.Text = user.AvatarUrl;
                WebsiteTextBox.Text = user.WebsiteUrl;
                SignatureTextBox.Text = user.Signature;
                GenderDropDownList.SelectedValue = ((byte)user.Gender).ToString();
                FirstNameTextBox.Text = user.FirstName;
                LastNameTextBox.Text = user.LastName;
                EmailTextBox.Text = user.Email;
                TopicsPerPageDropDownList.SelectedValue = user.TopicsPerPage.ToString();
                MessagesPerPageDropDownList.SelectedValue = user.MessagesPerPage.ToString();
                
                ThemeDropDownList.DataValueField = "Id";
                ThemeDropDownList.DataTextField = "Name";

                ThemeDropDownList.DataSource = ThemeService.GetAll();
                ThemeDropDownList.DataBind();

                ThemeDropDownList.Items.Insert(0, new ListItem("Forum value", "0"));

                if (user.Theme != null)
                {
                    ThemeDropDownList.SelectedValue = user.Theme.Id.ToString();
                }
                else
                {
                    ThemeDropDownList.SelectedValue = null;
                }
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (UserService.GetByIdAndPassword(CurrentSession.User.Id, PasswordTextBox.Text) != null)
            {
                if (EmailTextBox.Text.Length == 0)
                {
                    MasterPage.Messages.AddInformation(GetLocalResourceObject("EmailRequired").ToString());
                }
                else
                {
                    Theme theme = null;
                    if (Convert.ToByte(ThemeDropDownList.SelectedValue) != 0)
                    {
                        theme = ThemeService.GetById(Convert.ToByte(ThemeDropDownList.SelectedValue));
                    }

                    UserService.UpdateUser(
                        CurrentSession.User,
                        EmailTextBox.Text,
                        FirstNameTextBox.Text,
                        LastNameTextBox.Text,
                        (Gender)Enum.Parse(typeof(Gender), GenderDropDownList.SelectedValue),
                        DateTime.Now,
                        WebsiteTextBox.Text,
                        AvatarTextBox.Text,
                        SignatureTextBox.Text,
                        theme, 
                        byte.Parse(TopicsPerPageDropDownList.SelectedValue),
                        byte.Parse(MessagesPerPageDropDownList.SelectedValue)
                    );

                    MasterPage.Messages.AddInformation(GetLocalResourceObject("ProfileEdited").ToString());
                }
            }
            else
            {
                MasterPage.Messages.AddCritical(GetLocalResourceObject("PasswordIncorrect").ToString());
            }
        }
    }
}
